/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import org.brainstretch.recommend.UserProfile;
import org.codehaus.jackson.annotate.JsonIgnore;

/**
 *
 * @author mrodaitis
 */
@Entity
@XmlRootElement
@Table(name = "Users")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable=false)
    private String name;
    
    @Column(nullable = false)
    private String password;
    
    @OneToMany(fetch= FetchType.EAGER, cascade= CascadeType.PERSIST)
    private List<Achievement> achievements;
    
    @OneToMany(fetch= FetchType.EAGER)
    private List<SiteRole> roles;
    
    @OneToMany(mappedBy="user", cascade= CascadeType.ALL)
    private List<UserQuiz> quizzesTaken;
    
    @OneToOne
    private UserProfile profile;

    public UserProfile getProfile() {
        if(profile == null){
            profile = new UserProfile();
        }
        return profile;
    }

    public void setProfile(UserProfile profile) {
        this.profile = profile;
    }   
    
    public List<UserQuiz> getQuizzesTaken() {
        return quizzesTaken;
    }

    public void setQuizzesTaken(List<UserQuiz> quizzesTaken) {
        this.quizzesTaken = quizzesTaken;
    }

    public List<SiteRole> getRoles() {
        return roles;
    }

    public void setRoles(List<SiteRole> roles) {
        this.roles = roles;
    } 
    
    public List<Achievement> getAchievements() {
        return achievements;
    }

    public void setAchievements(List<Achievement> achievements) {
        this.achievements = achievements;
    }    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JsonIgnore
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }      
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public User() {
        achievements = new ArrayList<Achievement>();
    }

    
    
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.brainstretch.models.User[ id=" + id + " ]";
    }
    
}
